from DBA.dbmanager import DBInterface

class Salle:
	
	def __init__(self,id=""):
		self.id			= {"key":"ID","value":id}
		self.name		= {"key":"nom","value":""}
		#self.description	= {"key":"description","value":""}
		self.xposition		= {"key":"x","value":""}
		self.yposition		= {"key":"y","value":""}
		self.zposition		= {"key":"z","value":""}
		self.height		= {"key":"realheight","value":""}
		self.width		= {"key":"realwidth","value":""}
		self.urlPlan		= {"key":"url_plan","value":""}
		self.ensAccessPoint	= []
		self.__interfacebase	= DBInterface()
		if not id:
			self.id["value"]	= str(self.__interfacebase.createNewID());
	
	def createByDico(self,dico):
		self.name["value"] 	= dico[self.name["key"]]
		self.height["value"] 	= dico[self.height["key"]]
		self.xposition["value"] = dico[self.xposition["key"]]
		self.yposition["value"] = dico[self.yposition["key"]]
		self.zposition["value"] = dico[self.zposition["key"]]
		self.width["value"] 	= dico[self.width["key"]]
		self.urlPlan["value"] 	= dico[self.urlPlan["key"]]
	
	
	def addAccessPoint(self,AccessPoint):
		self.ensAccessPoint.append(AccessPoint)
	
	def nbAccessPoint(self):
		return self.ensAccessPoint.count()
	
	def selectAccessPointFromDB(self):
		key ={}
		key["link_sa"] = self.id["value"]
		self.ensSalles = self.__interfacebase.selectTable(self.__interfacebase.accessPointtablename,self.__interfacebase.accessPointmapping,key)
		return self.ensSalles

	def insertToDb(self,idBatiment):
		dico = self.createSalleDico(idBatiment)
		self.__interfacebase.insert(self.__interfacebase.salletablename,self.__interfacebase.sallemapping,dico)
		for AccessPoint in self.ensAccessPoint:
			AccessPoint.insertToDb(self.id["value"])

	def updateIntoDb(self,idBatiment):
		dico = self.createSalleDico(idBatiment)
		self.__interfacebase.update(self.__interfacebase.salletablename,self.__interfacebase.sallemapping,dico)
		for AccessPoint in self.ensAccessPoint:
			AccessPoint.updateIntoDb(self.id["value"])

	def createSalleDico(self,idBatiment):
		dico ={}
		dico[self.id["key"]]		= self.id["value"]
		dico["link_ba"]			= idBatiment
		dico[self.name["key"]]		= self.name["value"]
		#dico[self.description["key"]]	= self.description["value"]
		dico[self.xposition["key"]]	= self.xposition["value"]
		dico[self.yposition["key"]]	= self.yposition["value"]
		dico[self.zposition["key"]]	= self.zposition["value"]
		dico[self.height["key"]]	= self.height["value"]
		dico[self.width["key"]]		= self.width["value"]
		dico[self.urlPlan["key"]]	= self.urlPlan["value"]
		return dico

if __name__=="__main__":
	bat = Salle()	
	bat.name["value"]		= "test";
	#bat.description["value"]	= "t";
	bat.xposition["value"]		= "tz";
	bat.yposition["value"]		= "ta";
	bat.zposition["value"]		= "te";
	bat.height["value"]		= "tq";
	bat.width["value"]		= "t";
	
	print bat
